package com.taxx.dto;

import lombok.Data;

/**
 * 税务报送结果对象
 * 
 * @author taxx
 * @since 2024-01-01
 */
@Data
public class TaxReportResult {
    
    /**
     * 是否成功
     */
    private boolean success;
    
    /**
     * 错误原因
     */
    private String errorMessage;
    
    /**
     * 解密后的数据
     */
    private Object decryptedData;
    
    /**
     * HTTP状态码
     */
    private Integer httpStatusCode;
    
    /**
     * 原始响应体
     */
    private String originalResponse;
    
    /**
     * 业务返回码
     */
    private String returnCode;
    
    /**
     * 业务返回消息
     */
    private String returnMessage;
    
    /**
     * 请求ID
     */
    private String requestId;
    
    /**
     * 成功结果构造方法
     */
    public static TaxReportResult success(Object decryptedData, Integer httpStatusCode, String originalResponse) {
        TaxReportResult result = new TaxReportResult();
        result.setSuccess(true);
        result.setDecryptedData(decryptedData);
        result.setHttpStatusCode(httpStatusCode);
        result.setOriginalResponse(originalResponse);
        
        // 尝试从解密数据中提取业务信息
        if (decryptedData != null) {
            try {
                if (decryptedData instanceof java.util.Map) {
                    java.util.Map<?, ?> dataMap = (java.util.Map<?, ?>) decryptedData;
                    result.setReturnCode(String.valueOf(dataMap.get("returncode")));
                    result.setReturnMessage(String.valueOf(dataMap.get("returnmsg")));
                    result.setRequestId(String.valueOf(dataMap.get("requestId")));
                }
            } catch (Exception e) {
                // 忽略提取失败的情况
            }
        }
        
        return result;
    }
    
    /**
     * 失败结果构造方法
     */
    public static TaxReportResult failure(String errorMessage, Integer httpStatusCode, String originalResponse) {
        TaxReportResult result = new TaxReportResult();
        result.setSuccess(false);
        result.setErrorMessage(errorMessage);
        result.setHttpStatusCode(httpStatusCode);
        result.setOriginalResponse(originalResponse);
        return result;
    }
    
    /**
     * 异常失败结果构造方法
     */
    public static TaxReportResult failure(String errorMessage) {
        TaxReportResult result = new TaxReportResult();
        result.setSuccess(false);
        result.setErrorMessage(errorMessage);
        return result;
    }
    
    /**
     * 获取详细的错误信息
     */
    public String getDetailedErrorMessage() {
        if (success) {
            return "成功";
        }
        
        StringBuilder sb = new StringBuilder();
        if (errorMessage != null) {
            sb.append(errorMessage);
        }
        
        if (httpStatusCode != null && httpStatusCode != 200) {
            if (sb.length() > 0) sb.append("; ");
            sb.append("HTTP状态码: ").append(httpStatusCode);
        }
        
        if (returnCode != null && !"00".equals(returnCode)) {
            if (sb.length() > 0) sb.append("; ");
            sb.append("业务返回码: ").append(returnCode);
        }
        
        if (returnMessage != null) {
            if (sb.length() > 0) sb.append("; ");
            sb.append("业务返回消息: ").append(returnMessage);
        }
        
        return sb.length() > 0 ? sb.toString() : "未知错误";
    }
    
    /**
     * 判断是否为网络错误
     */
    public boolean isNetworkError() {
        return !success && (httpStatusCode == null || httpStatusCode == 0);
    }
    
    /**
     * 判断是否为业务错误
     */
    public boolean isBusinessError() {
        return !success && httpStatusCode != null && httpStatusCode == 200;
    }
    
    /**
     * 判断是否为HTTP错误
     */
    public boolean isHttpError() {
        return !success && httpStatusCode != null && httpStatusCode != 200;
    }
}
